package com.itheima.domain;

import org.springframework.cglib.beans.BeanCopier;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BeanUtiles
 * @Description:
 * @author: Yrd
 * @date: 2022-01-27 10:09
 */
public class BeanUtiles {
    public BeanUtiles() {
    }

    public static <T,V> void copy(T source,V target){
        if (null != source){
            BeanCopier beanCopier = CopierManager.getBeanCopier(source.getClass(), target.getClass(), false);
            beanCopier.copy(source,target,null);

        }
    }

    //对象是否包含属性
    private static boolean containsProperty(Object object,String[] names){
        //用来判断一个对象是否为一个类的实例
        if (List.class.isInstance(object)){
            return false;
        }else if(Map.class.isInstance(object)){
            return false;
        }else {
            boolean ret = false;
            if (object == null){
                return ret;
            }else {
                int i = 0;
                int length=0;
                for (length = names.length;i<length;++i){
                    try {
                        new PropertyDescriptor(names[i],object.getClass());
                    } catch (IntrospectionException e) {
                       break;
                    }
                }
                if (i==length){
                    ret = true;
                }
                return ret;
            }
        }
    }

    //设置bean的某个属性值(T 对象，K 属性 ，V 值)
    public static <T,K,V> void setProperty(T obj,K name, V value) throws Exception {
        //获取bean的某个属性的描述符
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor((String) name,obj.getClass());
        //获取用于写入属性值的方法
        Method writeMethod = propertyDescriptor.getWriteMethod();
        //写入属性值
        writeMethod.invoke(obj,value);
        System.out.println("set name");
    }

    //获取bean的某个属性值
    public static <T,K,V> void getProperty(T obj,K name) throws Exception {
        //获取Bean的某个属性的描述符
        PropertyDescriptor propertyDescriptor = new PropertyDescriptor((String) name, obj.getClass());
        //获取用于读取属性值的方法
        Method readMethod = propertyDescriptor.getReadMethod();
        //读取属性值
        Object objName = readMethod.invoke(obj);
        System.out.println("get name"+objName.toString());
    }

}


class UserInfo{
    private long userId;
    private String userName;
    private int age;
    private String emailAddress;

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
}




